﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: Combinate.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-09-06 11:39 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.Linq;

namespace BOL.Linq
{
    public static partial class Linq
    {
        public static IEnumerable<IEnumerable<TSource>> Combinate<TSource>(this IEnumerable<TSource> source, int k)
        {
            var list = source.ToList();
            if (k > list.Count || k < 1)
                throw new ArgumentOutOfRangeException("k");

            if (k == 0)
                yield return Enumerable.Empty<TSource>();

            foreach (var l in list)
                foreach (var c in Combinate(list.Skip(list.Count - k - 2), k - 1))
                    yield return c.Prepend(l);
        }
    }
}
